﻿using Furion.DependencyInjection;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc.Filters;
using Serilog;
using System.Threading.Tasks;

namespace Jiffy.Middle.Basics
{
    /// <summary>
    /// 全局异常日志
    /// </summary>
    public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton
    {
        public Task OnExceptionAsync(ExceptionContext context)
        {
            //var userContext = App.User;

            //Event.Emit("Log:Ex", new SysLogEx
            //{
            //    Account = userContext?.FindFirstValue(ClaimConst.CLAINM_ACCOUNT),
            //    Name = userContext?.FindFirstValue(ClaimConst.CLAINM_NAME),
            //    ClassName = context.Exception.TargetSite.DeclaringType?.FullName,
            //    MethodName = context.Exception.TargetSite.Name,
            //    ExceptionName = "全局异常",
            //    ExceptionMsg = context.Exception.Message,
            //    ExceptionSource = context.Exception.Source,
            //    StackTrace = context.Exception.StackTrace,
            //    ParamsObj = context.Exception.TargetSite.GetParameters().ToString(),
            //    ExceptionTime = DateTimeOffset.Now
            //});

            // 写日志文件
            Log.Error(context.Exception.ToString());

            return Task.CompletedTask;
        }
    }
}